package com.test.aqs;

/**
 * vloatile 只保证可见性，不保证原子性
 */
public class VolatileDemo {
    public static void main(String[] args) {
        MyData myData = new MyData();
        for (int i = 0; i < 20; i++) {
            new Thread(()->{
                for (int j = 0; j < 1000; j++) {
                    myData.dataPlus();
                }
            },String.valueOf(i)).start();
        }

        while (Thread.activeCount()>2){
            Thread.yield();
        }
        System.out.println(myData.get());
    }
}

class MyData{
    private volatile int i ;
    public void dataPlus(){
        i++;
    }
    public int get(){
        return i;
    }

}